def getContext(filepath,code):
    contexts = []
    f = open(filepath,"r",encoding=code)
    lines = f.readlines()
    for line in lines:
        if line != "\n" and line != "" and line != " \n" and line != "[toc]\n" and line.count("#") != 1:
            if "#" in line:
                contexts.append("___\n\n#"+line.strip("\n"))
                continue
            else:
                contexts.append(line.strip("\n"))
                continue
    return contexts
'''1.读取方括号内的内容
2.读取圆括号的内容
3.写入文件'''
path = "D:\\GitbookLibrary\\Discrete-Mathematics-and-Data-Structure\\"
f1 = open(path+"SUMMARY.md","r",encoding = "utf-8")
lines = f1.readlines()
f1.close()
files = []
titles = []
for line in lines:
    if "*" in line:
        titles.append(line[:line.rfind("]")].replace("    ","#").replace("* [","# "))
        files.append(path+line[line.rfind("(")+1:len(line)-2].replace("/","\\"))
        continue
f2 = open(path+"summary3.md","w",encoding="utf-8")
n = 0
while n < len(titles):
#while n < 5:
    f2.write("___\n"+titles[n]+"\n")
    for context in getContext(files[n],"utf-8"):
        f2.write(context+"\n")
    n += 1
f2.close()
'''
f2 = open(path+"summary3.md","r",encoding="utf-8")
texts = f2.readlines()
f2.close()
outputindexs = []
num = 0
while num +1 < len(texts):
    if texts[num] == texts[num+1]:
        print(num,texts[num])
        outputindexs.append(num+1)
        num += 1
        continue
    else:
        outputindexs.append(num)
        num += 1
        continue'''